package com.custom.taskmanager.service.biz;

import com.custom.action.condition.Conditions;
import com.custom.action.condition.LambdaConditionWrapper;
import com.custom.action.core.JdbcDao;
import com.custom.comm.date.DateTimeUtils;
import com.custom.comm.page.DbPageRows;
import com.custom.comm.utils.JudgeUtil;
import com.custom.taskmanager.entity.TaskRecord;
import com.custom.taskmanager.enums.TaskDifficultyEnum;
import com.custom.taskmanager.enums.TaskPriorityEnum;
import com.custom.taskmanager.enums.TaskProgressEnum;
import com.custom.taskmanager.params.TaskRecordRequest;
import com.custom.taskmanager.view.TaskRecordModel;
import com.custom.tools.biz.CustomProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author XiaoBai
 * @since 2023/9/7 0007 20:57
 */
@Service
public class QueryTaskListProcess implements CustomProcess<DbPageRows<TaskRecordModel>> {


    @Autowired
    private JdbcDao jdbcDao;

    private TaskRecordRequest request;

    private DbPageRows<TaskRecordModel> dbPageRows;

    private LambdaConditionWrapper<TaskRecordModel> wrapper;

    @Override
    public void doingBefore() {

    }

    @Override
    public void doingInit() {
        // 查询任务列表
        this.wrapper = Conditions.lambdaQuery(TaskRecordModel.class)
                .pageParams(request.getPageIndex(), request.getPageSize())

                // 模糊搜索，主要查询任务标题
                .like(JudgeUtil.isNotEmpty(request.getKeyword()), TaskRecordModel::getTaskTitle, request.getKeyword())

                // 若为1， 则查询开始时间
                .between(JudgeUtil.isValid(request.getTimeType()) && request.getTimeType() == 1, TaskRecordModel::getStartTime, request.getStartTime(), request.getEndTime())

                // 若为2， 则查询结束时间
                .between(JudgeUtil.isValid(request.getTimeType()) && request.getTimeType() == 2, TaskRecordModel::getEndTime, request.getStartTime(), request.getEndTime())

                // 查询当前进度
                .in(JudgeUtil.isNotEmpty(request.getCurrentProgress()), TaskRecord::getCurrentProgress, request.getCurrentProgress())

                // 查询任务难度
                .in(JudgeUtil.isNotEmpty(request.getDifficulty()), TaskRecordModel::getDifficulty, request.getDifficulty())

                // 查询任务的优先级
                .in(JudgeUtil.isNotEmpty(request.getPriority()), TaskRecordModel::getPriority, request.getPriority())

                // 查询任务是否过期
                .eq(TaskRecordModel::getExpireStatus, false)
                .orderByDesc(TaskRecordModel::getCreateTime);
    }

    @Override
    public void doingRunning() {
        this.dbPageRows = jdbcDao.selectPage(wrapper);
        if (dbPageRows.getData().isEmpty()) {
            return;
        }
        dbPageRows.getData().forEach(this::loadViewInfo);
    }

    private void loadViewInfo(TaskRecordModel row) {
        row.setDifficultyStr(TaskDifficultyEnum.getName(row.getDifficulty()));
        row.setCurrentProgressStr(TaskProgressEnum.getName(row.getCurrentProgress()));
        row.setPriorityStr(TaskPriorityEnum.getName(row.getPriority()));
        row.setStartTimeStr(DateTimeUtils.getFormatByTimeStamp(row.getStartTime()));
        row.setEndTimeStr(DateTimeUtils.getFormatByTimeStamp(row.getEndTime()));
        row.setCreateTimeStr(DateTimeUtils.getFormatByTimeStamp(row.getCreateTime()));
        row.setOperatorTimeStr(DateTimeUtils.getFormatByTimeStamp(row.getOperatorTime()));
    }

    @Override
    public void doingAfter() {

    }

    @Override
    public DbPageRows<TaskRecordModel> getResult() {
        return dbPageRows;
    }
}
